<style>
  .search-element-box-right .el-select{
    width: 100px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
<template>
  <el-row :gutter="15">
    <DialogMaster :visible.sync="visible" @refresh="refresh"></DialogMaster>
    <el-col :span="24">
      <el-card class="search-form">
        <el-form ref="searchForm" class="el-form-col" :model="search">
          <el-row :gutter="15" style="position:relative;">
            <el-col :lg="4" :md="6">
              <el-form-item prop="workerName" style="position: relative">
                <el-input placeholder="姓名" clearable v-model="search.workerName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="workerPhone" style="position: relative">
                <el-input placeholder="手机号" clearable v-model="search.workerPhone"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="addressArr">
                <CascadeAddress :level="2" v-model="search.addressArr" placeholder="服务范围"></CascadeAddress>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="businessCode">
                <el-select v-model="search.businessCode" clearable placeholder="业务类型">
                  <el-option
                    v-for="item in businessType"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

      </el-card>
    </el-col>
    <el-col :span="24">
      <el-card>
        <div slot="header" class="clearfix">
          <span>合作师傅列表</span>
          <el-button @click.prevent="showAddMaster" style="float: right; padding: 3px 0" type="text">新增师傅</el-button>
        </div>
        <el-table
          :data="mergeData"
          :span-method="objectSpanMethod"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column
            prop="index"
            width="60"
            align="center"
            label="#">
          </el-table-column>
          <el-table-column
            prop="workerName"
            width="120"
            align="center"
            label="姓名">
          </el-table-column>
          <el-table-column
            prop="workerPhone"
            width="120"
            align="center"
            label="手机号">
          </el-table-column>
          <el-table-column
            prop="provice/city"
            align="center"
            width="200"
            label="服务范围">
            <template slot-scope="scope">
              {{ scope.row.province || scope.row.provice }} / {{ scope.row.city }}
            </template>
          </el-table-column>
          <el-table-column
            prop="businessName"
            align="center"
            width="160"
            label="业务类型">
          </el-table-column>
          <el-table-column
            prop="serviceName"
            align="center"
            label="服务类型">
          </el-table-column>
          <el-table-column
            prop="foundDate"
            align="center"
            width="160"
            label="绑定时间">
            <template slot-scope="scope">
              {{ scope.row.foundDate | unixTimestampFilter }}
            </template>
          </el-table-column>
          <el-table-column
            prop="action"
            align="center"
            width="100"
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click.prevent="unbind(scope.row)">解除绑定</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-box pull-right" style="margin-top: 12px">
          <Pagination
            :total-item="totalItem"
            @pagination="pagination"
            @sizeChange="sizeChange"
            :perItem="search.pageSize"></Pagination>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>
<script>
import Pagination from '@/components/Pagination'
import CascadeAddress from '@/components/CascadeAddress'
import {unixTimestampFilter} from '@/filters'
import {copy, confirm, merge, dotData} from '@/utils'
import DialogMaster from '@/views/fhb/myMaster/DialogMaster'
import { getAllBusinessTypes } from '@/utils/selectOptions'
import lodash from 'lodash'
import {mapActions} from 'vuex'
export default {
  name: 'fhbMyMasterIndex',
  components: {
    DialogMaster,
    Pagination,
    CascadeAddress
  },
  filters: {
    unixTimestampFilter
  },
  data() {
    return {
      visible: false,
      searchLoading: false,
      isTrue: false,
      params: {
        pageSize: 20,
        pageNum: 1
      },
      tableData: [],
      search: {
        addressArr: [],
        businessCode: '',
        workerName: '',
        workerPhone: ''
      },
      totalItem: 0,
      businessType: []
    }
  },
  created() {
    this.httpGetTable()
    getAllBusinessTypes().then(arr => {
      this.businessType = arr
    })
  },
  computed: {
    mergeData() {
      return lodash.flatten(this.tableData.map((v, i) => {
        if (Array.isArray(v.servicesList)) {
          return v.servicesList.map((item, j) => Object.assign({}, item, v, {
            index: i + 1,
            showFlag: j === 0,
            mergeLength: v.servicesList.length
          }))
        } else {
          return Object.assign({}, v, {
            index: i + 1,
            showFlag: true,
            mergeLength: 1
          })
        }
      }))
    }
  },
  methods: {
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    searchFunc(search) {
      let params = copy(search, true)
      // 处理地址
      if (params.addressArr && params.addressArr.length) {
        params.provinceNo = dotData(params.addressArr, '[0]')
        params.cityNo = dotData(params.addressArr, '[1]')
        // params.provinceNo = params.addressArr[0].id
        // params.cityNo = params.addressArr[1].id
        delete params.addressArr
      } else {
        params.provinceNo = ''
        params.cityNo = ''
      }
      this.params = merge(this.params, params)
      this.refresh()
    },
    // 提交 解绑
    unbind(data) {
      confirm('你确定要和该师傅解除绑定吗？', '温馨提示', {
        type: 'warning',
        center: true,
        dangerouslyUseHTMLString: true,
        confirmButtonLoading: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          this.$ajax.post('/ms-fahuobao-order/FhbMyWorker/removeBinding', {id: data.id}).then(response => {
            if (response.success) {
              close().finally(_ => {
                confirm('解绑成功', undefined, {
                  type: 'success'
                })
              })
              // alert('解绑成功', '', 'success')
              this.refresh()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    },
    // 新增师傅弹框
    showAddMaster() {
      this.visible = true
    },
    refresh() {
      this.httpGetTable()
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 要合并的列 type:index
      let columnIndependent = ['businessName', 'serviceName']
      if (columnIndependent.indexOf(column.property) < 0) {
        if (row.showFlag) {
          return {
            rowspan: row.mergeLength,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    // 获取师傅数据
    httpGetTable() {
      this.searchLoading = true
      this.$ajax.table('/ms-fahuobao-order/FhbMyWorker/findMyWorkerList', this.params).then(response => {
        if (response.success) {
          this.tableData = response.data || []
          this.totalItem = response.totalItem
          console.log('this.tableData', this.tableData)
        }
      }).finally(_ => {
        this.searchLoading = false
      })
    },
    pagination(page) {
      this.params.pageNum = page
      this.httpGetTable()
    },
    // 页面大小变化
    sizeChange(size) {
      this.params.pageSize = size
      this.httpGetTable()
    },
    ...mapActions([
      'setRefreshRoute'
    ])
  }
}
</script>
